/**
 * 
 */
package cn.jhz.filesharingsystem.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import cn.jhz.filesharingsystem.model.Pager;
import cn.jhz.filesharingsystem.model.SystemContext;
import cn.jhz.filesharingsystem.model.User;
import cn.jhz.filesharingsystem.service.UserService;
import cn.jhz.filesharingsystem.util.HeadersUtil;
import cn.jhz.filesharingsystem.util.SecurityUtil;
import cn.jhz.filesharingsystem.web.AuthClass;
import cn.jhz.filesharingsystem.web.AuthMethod;

/**
 * @author asus
 *
 */
@AuthClass
@Controller
public class UserController {
	
	@Autowired
	private UserService userService;
	

	@AuthMethod
	@GetMapping("/user_management")
	private String usersManager(@RequestHeader(value="User-Agent") String userAgent) {
		if(HeadersUtil.checkAgentIsMobile(userAgent)) {
			return "mobileUserManagement";
		}
		return "userManagement";
	}		
	
	@AuthMethod
	@PostMapping("/user")
	private @ResponseBody String addUser(@RequestBody User user, @SessionAttribute(value = "loginUser")User loginUser) {
		try {
			if(!loginUser.getRole().getRoleName().equals("super")) {
				if(!loginUser.getRole().getRoleName().equals("admin"))
					throw new RuntimeException("权限不足!");
				if(loginUser.getRole().getState()==0 || loginUser.getState()==0)
					throw new RuntimeException("帐号已被禁用");
			}
			user.setPassword(SecurityUtil.md5$sha_x(user.getEmail(), user.getPassword()));
			userService.add(user);
		}catch (Exception e) {
			return "error";
		}
		return "ok";
	}
	
	@AuthMethod
	@DeleteMapping("/users")
	private @ResponseBody String deleteUsers(@SessionAttribute(value = "loginUser")User loginUser, @RequestBody Integer...ids) {
		try {
			if(!loginUser.getRole().getRoleName().equals("super")) {
				if(!loginUser.getRole().getRoleName().equals("admin"))
					throw new RuntimeException("权限不足!");
				if(loginUser.getRole().getState()==0 || loginUser.getState()==0)
					throw new RuntimeException("帐号已被禁用");
			}
			for(int id:ids) {
				userService.delete(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "ok";
	}
	
	@AuthMethod
	@PutMapping("/user")
	private @ResponseBody String updateUser(@RequestBody User user, @SessionAttribute(value = "loginUser")User loginUser) { 
		try {
			if(!loginUser.getRole().getRoleName().equals("super")) {
				if(!loginUser.getRole().getRoleName().equals("admin"))
					throw new RuntimeException("权限不足!");
				if(loginUser.getRole().getState()==0 || loginUser.getState()==0)
					throw new RuntimeException("帐号已被禁用");
			}
			
			userService.updateUser(user);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "ok";
	}
	
	@AuthMethod
	@GetMapping("/user")
	private @ResponseBody User getCurrentUser(@SessionAttribute(value = "loginUser")User loginUser) {
		return loginUser;
	}
	
	
	@AuthMethod
	@GetMapping("/users**")
	private @ResponseBody Pager<User> getUsersPage(@RequestParam(value = "page", required = false) Integer page
			,@RequestParam(value = "rows", required = false) Integer rows
			,@RequestParam(value="username",required=false) String username
			,@SessionAttribute(value = "loginUser")User loginUser
			,@SessionAttribute(value = "isadmin")boolean isadmin) {
		

		if(!loginUser.getRole().getRoleName().equals("super")) {
			if(!loginUser.getRole().getRoleName().equals("admin"))
				throw new RuntimeException("权限不足!");
			if(loginUser.getRole().getState()==0 || loginUser.getState()==0)
				throw new RuntimeException("帐号已被禁用");
		}		
		
		if(page != null && page > 0) SystemContext.setPageOffset((page-1)*rows);
		if(rows != null && rows > 0) SystemContext.setPageSize(rows);
		
		
		Pager<User> pager = null;
		if(isadmin) {
			pager = userService.getAllPagerUsers(username, false);
		}else{
			pager = userService.getAllPagerUsers(username, true);
		}	
	
		SystemContext.removePageOffset();
		SystemContext.removePageSize();
		return pager;
	}
}
